package com.yang.StackAndQueue;

import java.util.Deque;
import java.util.LinkedList;

public class LeetCode1047 {
    public static void main(String[] args) {
        String s = "abbaca";
        SolutionLeetCode1047 solutionLeetCode1047 = new SolutionLeetCode1047();
        String s1 = solutionLeetCode1047.removeDuplicates(s);
        System.out.println(s1);
    }
}

class SolutionLeetCode1047 {
    public String removeDuplicates(String s) {
        Deque<Character> stack = new LinkedList();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            // 栈为空时直接push 不用比较 否则会空指针异常
            if (stack.isEmpty() || stack.peek() != ch){
                stack.push(ch);
            }else {
                stack.pop();
            }
        }
        String string = "";
        while ( !stack.isEmpty() ){
            // 直接反过来就把顺序正确了
            string = stack.pop() + string;
        }
        return string;
    }
}
